package com.cruisecontrol4eclipse.core.handlers;

import java.net.URL;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.internal.browser.BrowserViewer;
import org.eclipse.ui.internal.browser.WebBrowserEditor;
import org.eclipse.ui.internal.browser.WebBrowserEditorInput;

@SuppressWarnings("restriction")
public abstract class AbstractBrowserEditorHandler<T> extends AbstractHandler {

	@SuppressWarnings("unchecked")
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
		if(currentSelection instanceof StructuredSelection) {
			WebBrowserEditorInput input = new WebBrowserEditorInput(getURL((T)((StructuredSelection)currentSelection).getFirstElement()),
					BrowserViewer.BUTTON_BAR | BrowserViewer.LOCATION_BAR | IWorkbenchBrowserSupport.STATUS);
			WebBrowserEditor.open(input);
		}
		return null;
	}

	abstract URL getURL(T element);
}
